package com.oa.controller;

import com.oa.back.ResponseBack;
import com.oa.common.annotion.Interceptor;
import com.oa.common.base.Pageable;
import com.oa.common.enums.CodeStatus;
import com.oa.service.DepartmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/manage/department")
public class DepartmentController extends BaseController {

    /** 日志输出类 **/
    private static final Logger logger= LoggerFactory.getLogger(DepartmentController.class);

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(value = "index.html",method = RequestMethod.GET)
    public String index(ModelMap modelMap, Pageable pageable,String key,String check){
        modelMap.addAttribute("page",departmentService.findPage(pageable,key));
        modelMap.addAttribute("pageable",pageable);
        modelMap.addAttribute("check",check);
        modelMap.addAttribute("key",key);
        return "/admin/department/index";
    }

    @RequestMapping(value = "submit",method = RequestMethod.POST)
    @ResponseBody
    @Interceptor(actionName = "提交部门的接口")
    public ResponseBack<String> submit(String params){
        try{
            return departmentService.submit(params);
        }catch (RuntimeException ex){
            logger.error("提交部门接口的异常:",ex);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    @Interceptor(actionName = "修改部门的接口")
    public ResponseBack<String> update(String params){
        try{
            return departmentService.update(params);
        }catch (RuntimeException ex){
            logger.error("修改部门接口的异常:",ex);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }

    @RequestMapping(value = "validate",method = RequestMethod.GET)
    @ResponseBody
    @Interceptor(actionName = "校验部门是否已经存在")
    public Boolean validate(String name,Long id){
        return !departmentService.validate(name, id);
    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    @Interceptor(actionName = "删除部门的接口")
    public ResponseBack<String> delete(Long id){
        try{
            return departmentService.deleteDepartment(id);
        }catch (RuntimeException ex){
            logger.error("删除部门接口的异常:",ex);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }
}
